package cn.com.sina.alan.oauth.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import redis.clients.jedis.JedisCluster;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by wanghongfei(hongfei7@staff.sina.com.cn) on 9/24/16.
 */
@Controller
@SessionAttributes("authorizationRequest")
public class ErrorCtr {
    private static final Logger log = LoggerFactory.getLogger(ErrorCtr.class);

    
    @Autowired
    private JedisCluster jedisCluster;
    
    @RequestMapping("/test")
    public @ResponseBody String ss(@RequestParam Map<String, String> parameters) {
    	jedisCluster.set("beadfed6-2de5-41bb-82e3-8c79e6cca513", "beadfed6-2de5-41bb-82e3-8c79e6cca513");
    	return jedisCluster.get("beadfed6-2de5-41bb-82e3-8c79e6cca513");
    }
    
    @RequestMapping("/oauth/error")
    public String error(@RequestParam Map<String, String> parameters) {
        String uri = parameters.get("redirect_uri");
        log.info("重定向: {}", uri);

        return "redirect:" + uri + "?error=1";
    }
    
    @RequestMapping(value="/logout")  
    public String logoutPage (HttpServletRequest request, HttpServletResponse response) {  
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();  
        if (auth != null){      
            new SecurityContextLogoutHandler().logout(request, response, auth);  
        }  
        return "redirect:/login?logout";  
    }  

}
